package chabernac.android.wordbattle.paint.crosstile;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;

public class FullCrossTileDrawable extends AbstractCrossTileDrawable {

	private final Letter myLetterUpLeft;
	private final Letter myLetterUpRight;
	private final Letter myLetterDownLeft;
	private final Letter myLetterDownRight;
	
	public FullCrossTileDrawable(
			Word aSelectedWord, 
			Letter aLetterUpLeft, 
			Letter aLetterUpRight,
			Letter aLetterDownLeft, 
			Letter aLetterDownRight) 
	{
		super(aSelectedWord);
		myLetterUpLeft = aLetterUpLeft;
		myLetterUpRight = aLetterUpRight;
		myLetterDownLeft = aLetterDownLeft;
		myLetterDownRight = aLetterDownRight;
	}
	
	@Override
	protected Bitmap getBitmap(WordBattleView aWordBattleView)
	{
		SvgResourceNameBuilder aBuilder = new SvgResourceNameBuilder();
		// Add selection string of up left
		addSvgResourceNameSelectionString(aBuilder, myLetterUpLeft);
		// Add selection string of up right
		addSvgResourceNameSelectionString(aBuilder, myLetterUpRight);
		// Add selection string of down left	
		addSvgResourceNameSelectionString(aBuilder, myLetterDownLeft);
		// Add selection string of down right	
		addSvgResourceNameSelectionString(aBuilder, myLetterDownRight);
		// return the cross tile bitmap
		return CrossTileBitmapFactory.getInstance().getCrossTileBitmap(aBuilder.getResourceName(), aWordBattleView);
	}
	
	@Override
	protected Point getDrawingPoint(WordBattleView aWordBattleView){
		int x = WordBattleView.BORDER_SIZE;
		x += (int)(myLetterUpLeft.getColumn() * aWordBattleView.getWidthPerColumn());
		x += (int)(aWordBattleView.getWidthPerColumn()/2);
		int y = WordBattleView.BORDER_SIZE;
		y += (int)(myLetterUpLeft.getRow() * aWordBattleView.getHeightPerRow());
		y += (int)(aWordBattleView.getHeightPerRow()/2);
		return new Point(x,y);
	}
	
	@Override
	protected Rect getBitmapSubsetRect(Bitmap aBitmap)
	{
		return new Rect(0, 0, (int)aBitmap.getWidth(), (int)aBitmap.getHeight());
	}
	
	private void addSvgResourceNameSelectionString(SvgResourceNameBuilder aBuilder, Letter aLetter)
	{
		if (addSvgResourceNameSelectionStringSingleLetter(aBuilder, aLetter))
		{
			Letter aNextLetter = mySelectedWord.getNextSelectedLetter(aLetter);
			addSvgResourceNameSelectionStringSingleLetter(aBuilder, aNextLetter);
			Letter aPreviousLetter = mySelectedWord.getPreviousSelectedLetter(aLetter);
			addSvgResourceNameSelectionStringSingleLetter(aBuilder, aPreviousLetter);
			aBuilder.appendSelectionBreak();
		}
	}

	private boolean addSvgResourceNameSelectionStringSingleLetter(SvgResourceNameBuilder aBuilder, Letter aLetter)
	{
		if ( aLetter == null ) return false;
		boolean aLetterSelected = mySelectedWord.contains(aLetter);
		if(aLetterSelected == true)
		{
			if (aLetter == myLetterUpLeft)
			{
				aBuilder.appendSelectStringUpLeft();
			}
			else if (aLetter == myLetterUpRight)
			{
				aBuilder.appendSelectStringUpRight();
			}
			else if (aLetter == myLetterDownLeft)
			{
				aBuilder.appendSelectStringDownLeft();
			}
			else if (aLetter == myLetterDownRight)
			{
				aBuilder.appendSelectStringDownRight();
			}
		}
		return aLetterSelected;
	}

}
